<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="角色名称：">
        <el-input v-model.trim="query.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        <el-button
          v-if="!roleIds"
          icon="el-icon-circle-plus-outline"
          type="primary"
          @click="handleAdd"
          >新增</el-button
        >

        <el-button
          v-if="roleIds"
          icon="el-icon-circle-plus-outline"
          type="success"
          @click="handleUserRole"
          >设置角色</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 :data 绑定渲染的数据, border 纵向边框 -->
    <el-table
      ref="tableRef"
      :data="list"
      row-key="id"
      border
      highlight-current-row
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <!-- type="index"获取索引值，从1开始 ，label显示标题，prop 数据字段名，width列宽 -->
      <el-table-column reserve-selection type="selection" width="55">
      </el-table-column>
      <el-table-column align="center" type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column align="center" prop="name" label="角色名称">
      </el-table-column>
      <el-table-column align="center" prop="remark" label="备注">
      </el-table-column>
      <el-table-column align="center" label="操作" v-if="!roleIds">
        <template slot-scope="scope">
          <el-button
            @click="handlePermission(scope.row.id)"
            size="mini"
            type="success"
          >
            分配权限
          </el-button>
          <el-button @click="handleEdit(scope.row.id)" size="mini">
            编辑
          </el-button>
          <el-button
            @click="handleDelete(scope.row.id)"
            size="mini"
            type="danger"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pageination
      :currentPage="page.current"
      :pageSizeInfo="page.size"
      :total="page.total"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <!-- 弹窗 -->
    <edit
      :dialogVisible="edit.dialogVisible"
      :title="edit.title"
      :formData="edit.formData"
      :remoteForm="remoteForm"
    />
    <!-- 权限弹窗 -->
    <permission
      :dialogVisible="per.dialogVisible"
      title="分配权限"
      :remotePerForm="remotePerForm"
      :roleId="per.roleId"
    />
  </div>
</template>

<script>
import { ApiGetRoleList, ApiDeleteAdvertId, ApiGetRoleId } from '@/api/role'
import pageination from '@/components/common/pageination.vue'
import edit from './components/edit.vue'
import permission from './components/permission.vue'
export default {
  name: 'Role',
  components: {
    pageination,
    edit,
    permission
  },
  // 当用户管理模块, 将当前这个组件文件作为子组件时,进行接收父组件传递过来的属性
  props: {
    roleIds: null
  },
  data () {
    return {
      list: [],
      selectFalse: false, // 控制按钮显示隐藏
      labelArr: ['角色名称',],
      page: {
        current: 1,
        size: 20,
        total: 0
      },
      query: {},
      edit: {
        dialogVisible: false,
        title: '新增',
        formData: {}
      },
      per: { //分配权限
        dialogVisible: false,
        roleId: null, // 角色id
      },
      handelIdsList: [] // 勾选的角色用户数据
    }
  },
  methods: {
    async fetchData () {
      const { data: res } = await ApiGetRoleList(this.query, this.page.size, this.page.current)
      this.list = res.records
      this.page.total = res.total
      // 勾选角色
      this.checkRoles()
    },
    checkRoles () {
      // 清空上一次的数据
      this.$refs.tableRef.clearSelection();
      if (this.roleIds) {
        // 存在ids
        this.list.forEach(item => {
          // ids与对应id相匹配
          if (this.roleIds.indexOf(item.id) !== -1) {
            this.$refs.tableRef.toggleRowSelection(item);
          }
        })
      }
    },
    queryData () { // 文章查询
      this.fetchData()
    },
    handlePermission (id) { //分配权限
      console.log('分配权限', id)
    },
    handleAdd () { // 新增操作
      this.edit.title = '新增'
      this.edit.dialogVisible = true
    },
    handleUserRole () { // 设置角色
      // 遍历以选中的角色
      const ceckIds = []
      this.handelIdsList.forEach(item => {
        ceckIds.push(item.id)
      })
      // 传递事件给父组件
      this.$emit('saveUserRole', ceckIds)
    },
    // 勾选的角色id
    handleSelectionChange (val) {
      this.handelIdsList = val
    },
    async handleEdit (id) { // 编辑操作
      const res = await ApiGetRoleId(id)
      if (res.code === 20000) {
        this.edit.formData = res.data
        this.edit.title = '编辑'
        this.edit.dialogVisible = true
      }
    },
    handleDelete (id) { // 删除操作
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await ApiDeleteAdvertId(id)
        this.$message({
          type: res.code === 20000 ? 'success' : 'warn',
          message: res.message
        });
        // 刷新列表
        this.fetchData()
      }).catch(() => {

      });
    },
    reload () { // 重置
      this.query = {}
      this.fetchData()
    },
    // 当前条数
    handleSizeChange (val) {
      this.page.size = val
      this.fetchData()
    },
    // 当前页数
    handleCurrentChange (val) {
      this.page.current = val
      this.fetchData()
    },
    remoteForm () { // 关闭弹窗
      this.edit.formData = {}
      this.edit.dialogVisible = false
      this.fetchData()
    },
    handlePermission (id) { // 分配权限按钮
      this.per.roleId = id
      this.per.dialogVisible = true
    },
    remotePerForm () { // 关闭权限
      this.per.roleId = null
      this.per.dialogVisible = false
      this.fetchData()
    }
  },
  created () {
    this.fetchData()
  },
}
</script>

<style>
</style>